package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.userMapper;
import com.jt.pojo.User;
import com.jt.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.UUID;

@Service
public class userServiceImpl implements userService {
    @Autowired
    private userMapper userMapper;



    @Override
    @Transactional
    public Integer updateUser(User user) {
        return userMapper.updateById(user);
    }

    @Override
    public User fandByUserId(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    @Transactional
    public Integer deleteUser(Integer id) {
        return userMapper.deleteById(id);
    }

    @Override
    @Transactional
    public Integer addUser(User user) {
        String pwd = user.getPassword();
        String md5has = DigestUtils.md5DigestAsHex(pwd.getBytes());//md5加密
        user.setPassword(md5has);

        user.setStatus(false);
        return userMapper.insert(user);
    }

    @Override
    @Transactional
    public Integer updateState(User user) {
        return userMapper.updateById(user);
    }

    @Override
    public List<User> findAll() {
        return userMapper.selectList(null);
    }

    @Override
    public String login(User user) {
        String pwd = user.getPassword();
        String md5has = DigestUtils.md5DigestAsHex(pwd.getBytes());//md5加密
        user.setPassword(md5has);

        //查询数据库
        QueryWrapper<User> wrapper = new QueryWrapper<>(user);
        User udata = userMapper.selectOne(wrapper);
        String token = UUID.randomUUID().toString().replace("-", "");
        return udata != null ? token : null;
    }

    /* Sql: select * from user limit 数据起始位置,查询条数
     * 查询第1页 每页20条:
     *      select * from user limit 0,20
     * 查询第2页 每页20条:
     *      select * from user limit 20,20
     * 查询第3页 每页20条:
     *      select * from user limit 40,20
     * 查询第N页 每页20条:
     *      select * from user limit (n-1)20,20
     *      SELECT * FROM USER LIMIT (n-1)m, m 第N页取M条
     *  带查询条件的Sql:
     *       select * from user where name like "%#{query}%" limit (n-1)20,20
     */
    @Override
    public PageResult getUserList(PageResult pageResult) {
        //1定义分页对象
        IPage<User> page = new Page<>(pageResult.getPageNum(), pageResult.getPageSize());
        //2.构建查询的条件构造器
        QueryWrapper<User> Wrapper = new QueryWrapper<>();
        boolean flag = StringUtils.hasLength(pageResult.getQuery());
        //根据条件判断是否拼接where条件
        Wrapper.like(flag, "username", pageResult.getQuery());
        //MP提供的分页查询的方法,返回page分页对象，包含其他分页的数据结果
        page = userMapper.selectPage(page, Wrapper);
        long total = page.getTotal();//总数
        List<User> list = page.getRecords();//分页结果
        return pageResult.setRows(list).setTotal(total);
    }
}
